#include "bsp_debug_uart.h"

#define DEBUG_UART_CTL  g_uart3_ctrl
#define DEBUG_UART_CFG  g_uart3_cfg

void Debug_UART_Init(void)
{
    fsp_err_t err = FSP_SUCCESS;
    
    err = R_SCI_B_UART_Open (&DEBUG_UART_CTL, &DEBUG_UART_CFG);
    assert(FSP_SUCCESS == err);
}



volatile bool uart_send_complete_flag = false;


void debug_uart_callback (uart_callback_args_t * p_args)
{
    switch (p_args->event)
    {
        case UART_EVENT_RX_CHAR:
        {
            break;
        }
        case UART_EVENT_TX_COMPLETE:
        {
            uart_send_complete_flag = true;
            break;
        }
        default:
            break;
    }
}


/* redirect printf */
#if defined __GNUC__ && !defined __clang__
int _write(int fd, char *pBuffer, int size)
{
    (void)fd;
    R_SCI_B_UART_Write(&DEBUG_UART_CTL, (uint8_t *)pBuffer, (uint32_t)size);
    while(uart_send_complete_flag == false);
    uart_send_complete_flag = false;
    return size;
}
#else
int fputc(int ch, FILE *f)
{
    (void)f;
    R_SCI_B_UART_Write(&DEBUG_UART_CTL, (uint8_t *)&ch, 1);
    while(uart_send_complete_flag == false);
    uart_send_complete_flag = false;
    return ch;
}
#endif





